<template>
    <div class='approval'>
      <el-card class="box-card">
        <div class="title">{{$t('electricOrder.lbl11')}}</div>
        <div class="recent-data">
          <!-- 今日已分配数量 -->
          <div class="recent-item" @click="handleRencentDataClick(2)">
            <div>
              <b>{{recentData.TODAY_ALLOCATED_COUNT}}</b>
            </div>
            <div>{{$t('electricOrder.lbl1')}}</div>
          </div>
          <!-- 今日已完成数量 -->
          <div class="recent-item" @click="handleRencentDataClick(3)">
            <div>
              <b>{{recentData.TODAY_HAND_ALLOCATED_COUNT}}</b>
            </div>
            <div>{{$t('electricOrder.lbl2')}}</div>
          </div>
          <!-- 历史已分配数量 -->
          <div class="recent-item" @click="handleRencentDataClick(4)">
            <div>
              <b>{{recentData.HISTORY_ALLOCATED_COUNT}}</b>
            </div>
            <div>{{$t('electricOrder.lbl4')}}</div>
          </div>
          <!-- 历史已完成数量 -->
          <div class="recent-item" @click="handleRencentDataClick(5)">
            <div>
              <b>{{recentData.HISTORY_HAND_ALLOCATED_COUNT}}</b>
            </div>
            <div>{{$t('electricOrder.lbl5')}}</div>
          </div>
        </div>
      </el-card>
        <el-card class="box-card">
            <div class='title'>
                {{$t('approvalDispatch.title1')}}
            </div>
            <div class='menu-search'> 
                <div class='menu-title'>
                  <!-- 分配时间 -->
                  <div class='searchItem searchItem-date'>
                    <span>{{$t('electricOrder.lbl6')}}</span>
                    <el-date-picker
                    size='small'
                    v-model="listQuery.startDate"
                    type="date"
                    value-format="yyyy-MM-dd"
                    :placeholder="$t('rule.beginDate')">
                    </el-date-picker>
                    <b>{{$t('approval.go')}}</b>
                    <el-date-picker
                    size='small'
                      v-model="listQuery.endDate"
                      type="date"
                      value-format="yyyy-MM-dd"
                      :placeholder="$t('rule.endDate')">
                    </el-date-picker>
                  </div>
                  <!-- 业务渠道 -->
                  <div class='searchItem'>
                    <span>{{$t('electricOrder.col7')}}</span>
                    <el-select v-model='listQuery.channel' size='small'
                      @change="handleChannelChange">
                      <el-option
                      v-for='item in channelList'
                      :key='item.channel'
                      :label='item.org_name'
                      :value='item.channel'>
                      </el-option>
                    </el-select>
                  </div>
                  <!-- 电销组 -->
                  <div class='searchItem'>
                    <span>{{$t('electricOrder.lbl7')}}</span>
                    <el-select 
                    v-model='listQuery.salesGroupId' 
                    size='small'
                   >
                      <el-option
                      v-for='item in salesGroupIds'
                      :key='item.label'
                      :label='item.value'
                      :value='item.label'>
                      </el-option>
                    </el-select>
                  </div>
                  <!-- 电销员 -->
                  <div class='searchItem'>
                    <span>{{$t('electricOrder.lbl8')}}</span>
                    <el-select v-model='listQuery.salesGroupMemberId' size='small'>
                      <el-option
                      v-for='item in salesGroupMemberIds'
                      :key='item.label'
                      :label='item.value'
                      :value='item.label'>
                      </el-option>
                    </el-select>
                  </div>
                 <!-- 电销状态 -->
                  <div class='searchItem'>
                    <span>{{$t('electricOrder.lbl9')}}</span>
                    <el-select v-model='listQuery.salesStatus' size='small'>
                      <el-option
                      v-for='item in statusList'
                      :key='item.id'
                      :label='item.dictValue'
                      :value='item.dictCode'>
                      </el-option>
                    </el-select>
                  </div>
                  <!-- 手机号 -->
                  <div class="searchItem">
                    <span>{{$t('approval.phoneNo')}}</span>
                    <el-input v-model="listQuery.userPhone" :placeholder="$t('rule.phoneNo')" size="small"></el-input>
                  </div>
                </div>
                <div class='menu-title-btn'>
                  <button type='button' @click='search'>
                      {{$t('btn.query')}}
                  </button>
                  <button type='button' @click='reset'>
                      {{$t('btn.reset')}}
                  </button>
                  <button type='button' @click='exportFn' v-if="electricOrder_btn_export">
                      {{$t('btn.export')}}
                  </button>
              </div>
            </div>
        </el-card>
        <el-card class='box-card'>
            <div class='title'>
                {{$t('electricOrder.lbl10')}}
            </div>
            <el-table
                v-loading="listLoading"
                ref='multipleTable'
                :data='tableData'
                tooltip-effect='dark'
                style='width: 100%'
                @selection-change='handleSelectionChange'
                border
                size='mini'>
                <el-table-column :label="$t('user.num')" header-align="center" align="center" width="60">
                  <template slot-scope="scope">
                    <span>{{scope.row.index}}</span>
                  </template>
                </el-table-column>
                <!-- 客户姓名 -->
                <el-table-column
                prop='userRealName' align="center" header-align="center" 
                :label="$t('electricOrder.col12')"
                width='140'>
                </el-table-column>
                <!-- 手机号 -->
                <el-table-column
                prop='userPhone' align="center" header-align="center" 
                :label="$t('electricOrder.col13')"
                width='180'>
                </el-table-column>
                <!-- 上一笔结清业务ID -->
                <el-table-column
                prop='lastFiveOrderId' align="center" header-align="center" 
                :label="$t('electricOrder.col14')"
                width='180'>
                </el-table-column>
                <!-- 结清本金(RP) -->
                <el-table-column
                prop='lentAmt' align="center" header-align="center" 
                :label="$t('electricOrder.col15')"
                :formatter="cashFormatter"
                width='140'>
                </el-table-column>
                <!-- 借款期限 -->
                <el-table-column
                prop='pTerm' align="center" header-align="center" 
                :label="$t('electricOrder.col16')"
                width='100'>
                </el-table-column>
                <!-- 业务渠道 -->
                <el-table-column
                prop="channelName" align="center" header-align="center" 
                :label="$t('electricOrder.col17')"
                width='200'>
                </el-table-column>
                <!-- 借款APP -->
                <el-table-column
                prop='appPackageName' align="center" header-align="center" 
                :label="$t('electricOrder.col18')"
                width='200'>
                </el-table-column>
                <!-- 用户状态 -->
                <el-table-column
                prop='saleStatusValue' align="center" header-align="center" 
                :label="$t('electricOrder.col19')"
                width='200'>
                </el-table-column>
                <!-- 客户等级 -->
                <el-table-column
                prop='desireLevelValue' align="center" header-align="center" 
                :label="$t('electricOrder.col20')"
                width='200'>
                </el-table-column>
                <!-- 电销组 -->
                <el-table-column
                prop='groupName' align="center" header-align="center" 
                :label="$t('electricOrder.col21')"
                width='200'>
                </el-table-column>
                <!-- 电销元 -->
                <el-table-column
                prop='checkUserName' align="center" header-align="center" 
                :label="$t('electricOrder.col22')"
                width='200'>
                </el-table-column>
                <!-- 电销状态 -->
                <el-table-column
                prop='telStatus' align="center" header-align="center" 
                :label="$t('electricOrder.col23')"
                width='200'>
                </el-table-column>
                <!-- 分配时间 -->
                <el-table-column
                prop='distributionTime' align="center" header-align="center" 
                :label="$t('electricOrder.col24')"
                width='200'>
                </el-table-column>
                <!-- 备注 -->
                <el-table-column
                prop='checkRemark' align="center" header-align="center" 
                :label="$t('electricOrder.col25')"
                width='200'>
                </el-table-column>
                <!-- 外部导入备注 -->
                <el-table-column
                prop='note' align="center" header-align="center" 
                :label="$t('electricOrder.outsideDesc')"
                width='200'>
                </el-table-column>
            </el-table>
            <div class="pagination-container">
              <el-pagination
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
              :current-page="listQuery.page"
              :page-sizes="[5, 10, 20, 30]"
              :page-size="listQuery.limit"
              layout="total, sizes, prev, pager, next, jumper"
              :total="total">
              </el-pagination>
            </div>
        </el-card>
    </div>
</template>

<script>
import { mapGetters } from 'vuex';
import { dataMasking, formatCash } from '@/utils/index'
import { getChannels, getStatus, getGroup, getGroupMember, getAllocatedPage, allocatedPageExport } from '@/api/dianxiao/index'
export default {
  name: 'DistributedOrder',
  data() {
    return {
      listLoading: false,
      total: null,
      recentData: {
        // 今日已分配数量
        TODAY_ALLOCATED_COUNT: 0,
        // 今日已完成数量
        TODAY_HAND_ALLOCATED_COUNT: 0,
        // 历史已分配数量
        HISTORY_ALLOCATED_COUNT: 0,
        // 历史已完成数量
        HISTORY_HAND_ALLOCATED_COUNT: 0
      },
      listQuery: {
        limit: 5,
        page: 1,
        // 渠道
        channel: '',
        // "1","需要导出","2","不需要导出"
        exportType: '',
        // "1","默认查询分页",
        // "2","今日已分配订单分页",
        // "3","今日完成订单分页",
        // "4","历史已分配订单分页",
        // "5","历史完成订单分
        pageType: '',
        // 以上是必传参数
        // 开始时间
        startDate: '',
        // 结束时间
        endDate: '',
        // 电销组ID
        salesGroupId: '',
        // 电销人ID
        salesGroupMemberId: '',
        // 电销状态
        salesStatus: '',
        // 手机号
        userPhone: '',
      },
      channelList: [],
      tableData: [],
      multipleSelection: [],
      dialogFormVisible: false,
      statusList: [],
      // 电销组ID
      salesGroupIds: [],
      // 电销人ID
      salesGroupMemberIds: [],
      electricOrder_btn_export: false
    };
  },
  computed: {
    ...mapGetters(['elements'])
  },
  created() {
    this.electricOrder_btn_export = this.elements['electricOrder:btn_export']
    // this.initDate()
    getChannels().then(res => {
      if (res.status === 200 && res.data && res.data.length > 0) {
        this.channelList = this.channelList.concat(res.data)
        this.listQuery.channel = this.channelList[0].channel
        this.search()
        if (this.channelList[0].channel) {
          this.handleChannelChange(this.channelList[0].channel)
        }
      }
    })

    getStatus().then(res => {
      if (res.status === 200) {
        this.statusList = res.data
      }
    })
  },
  methods: {
    handleChannelChange(val) {
      this.listQuery.salesGroupId = ''
      this.listQuery.salesGroupMemberId = ''
      getGroup(val).then(res => {
        if (res.status === 200) {
          this.salesGroupIds = res.data
        }
      })
      getGroupMember(val).then(res => {
        if (res.status === 200) {
          this.salesGroupMemberIds = res.data
        }
      })
    },
    handleRencentDataClick(pageType) {
      this.listQuery.page = 1;
      this.listQuery.pageType = pageType
      this.listQuery.exportType = '2'
      this.getList()
    },
    getTelStatusName(key) {
      let statusName = ''
      for (let i = 0; i < this.statusList.length; i++) {
        const item = this.statusList[i]
        if (key === parseInt(item.dictCode)) {
          statusName = item.dictValue
          break
        }
      }

      return statusName
    },
    getList() {
      this.listLoading = true;
      getAllocatedPage(this.listQuery).then(res => {
        this.listLoading = false
        if (res.status === 200) {
          const data = res.data
          // 近期数据
          Object.keys(this.recentData).forEach(item => {
            data.allocatedPageCounts.forEach(el => {
              if (item === el.key) {
                this.recentData[item] = el.count
              }
            })
          })
          const rows = data.rows
          rows.forEach((item, index) => {
            item.index = index + (this.listQuery.page - 1) * this.listQuery.limit + 1
            item.telStatus = this.getTelStatusName(item.telStatus)
          })
          // 列表
          this.total = parseInt(data.total)
          this.tableData = rows
        }
      })
    },
    // 查询
    search() {
      this.listQuery.page = 1;
      this.listQuery.exportType = '2'
      this.listQuery.pageType = '1'
      this.getList();
    },
    // 重置
    reset() {
      this.listQuery.limit = 5;
      this.listQuery.page = 1;
      // this.listQuery.channel = ''
      this.listQuery.channel = this.channelList[0].channel
      this.listQuery.exportType = '2'
      this.listQuery.pageType = '1'
      this.listQuery.startDate = ''
      this.listQuery.endDate = ''
      this.listQuery.salesGroupId = ''
      this.listQuery.salesGroupMemberId = ''
      this.listQuery.salesStatus = ''
      this.listQuery.userPhone = ''
    },
    // 导出
    exportFn() {
      this.listLoading = true
      this.listQuery.exportType = '1'
      this.listQuery.pageType = '1'
      allocatedPageExport(this.listQuery).then(res => {
        const data = res.data
        const rows = data.rows
        rows.forEach((item, index) => {
          item.index = index + (this.listQuery.page - 1) * this.listQuery.limit + 1
          item.telStatus = this.getTelStatusName(item.telStatus)
          if (item.userRealName) {
            item.userRealName = dataMasking(item.userRealName)
          }
          if (item.userPhone) {
            item.userPhone = dataMasking(item.userPhone)
          }
        })
        require.ensure([], () => {
          const { export_json_to_excel } = require('@/vendor/Export2Excel')
          const tHeader = [
            this.$t('electricOrder.col12'),
            this.$t('electricOrder.col13'),
            this.$t('electricOrder.col14'),
            this.$t('electricOrder.col15'),
            this.$t('electricOrder.col16'),
            this.$t('electricOrder.col17'),
            this.$t('electricOrder.col18'),
            this.$t('electricOrder.col19'),
            this.$t('electricOrder.col20'),
            this.$t('electricOrder.col21'),
            this.$t('electricOrder.col22'),
            this.$t('electricOrder.col23'),
            this.$t('electricOrder.col24'),
            this.$t('electricOrder.col25')
          ]
          const filterVal = [
            'userRealName',
            'userPhone',
            'lastFiveOrderId',
            'lentAmt',
            'pTerm',
            'channelName',
            'appPackageName',
            'saleStatusValue',
            'desireLevelValue',
            'groupName',
            'checkUserName',
            'telStatus',
            'distributionTime',
            'checkRemark'];
          const data = this.formatJson(filterVal, rows)
          export_json_to_excel(tHeader, data, this.$t('customerService.distributedOrder'))
          this.listLoading = false;
        })
      })
    },
    formatJson(filterVal, jsonData) {
      return jsonData.map(v => filterVal.map(j => v[j]))
    },
    // 日期初始化
    initDate() {
      this.listQuery.startDate = this.GetDateStr(-7);
      this.listQuery.endDate = this.GetDateStr(-1);
    },
    GetDateStr(AddDayCount) {
      const dd = new Date();
      dd.setDate(dd.getDate() + AddDayCount);// 获取AddDayCount天后的日期
      const y = dd.getFullYear();
      const m = (dd.getMonth() + 1) < 10 ? '0' + (dd.getMonth() + 1) : dd.getMonth() + 1 // 获取当前月份的日期，不足10补0
      const d = dd.getDate() < 10 ? '0' + dd.getDate() : dd.getDate(); // 获取当前几号，不足10补0
      return y + '-' + m + '-' + d;
    },
    cashFormatter(row, col, cellValue) {
      return formatCash(cellValue)
    },
    // 选中的数据
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    handleSizeChange(val) {
      this.listQuery.limit = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.listQuery.page = val;
      this.getList();
    }
  }
};
</script>

<style scoped lang='scss'>
.title {
  line-height: 30px;
  font-size: 16px;
  display: flex;
  align-items: center;
  font-weight: bold;
}
.title:before{
  display: block;
  content: "";
  width: 3px;
  height: 16px;
  background-color: #309afe;
}
.title:before {
  margin-right: 8px;
}
::before {
  box-sizing: inherit;
  -webkit-tap-highlight-color: transparent;
}
.menu-title {
  width: 100%;
  box-sizing: border-box;
  background: #ffffff;
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  .searchItem {
    display: flex;
    align-items: center;
    padding: 10px  0;
    span {
      display: inline-block;
      width: 100px;
      text-align: right;
      font-size: 12px;
      color: #5f5f61;
      padding-right: 5px;
    }
    div:nth-child(2) {
      width: 150px;
    }
  }
  .searchItem-date {
    display: flex;
    align-items: center;
    padding: 10px  0;
    b {
      margin: 0 10px;
      font-weight: normal;
      font-size: 12px;
      color: #5f5f61;
    }
    div {
       width: 150px;
    }
  }
}
.menu-title-btn {
  padding: 10px 0;
  box-sizing: border-box;
  button {
    padding: 0 20px;
    height: 28px;
    background: #20a0ff;
    color: #fff;
    outline: none;
    border-width: 0;
    border-radius: 4px;
    font-size: 12px;
    margin-left: 10px;
    &:nth-child(1) {
      margin-left: 0;
    }
    &:nth-child(2) {
      background: #ebf5ff;
      color: #20a0ff;
      border: 1px solid #abd5ff;
    }
  }
}
.menu-list {
  padding: 20px 0;
  box-sizing: border-box;
  button {
    padding: 0 20px;
    height: 28px;
    background: #20a0ff;
    color: #fff;
    outline: none;
    border-width: 0;
    border-radius: 4px;
    font-size: 12px;
    margin-left: 10px;
    &:nth-child(1) {
      margin-left: 0;
    }
  }
  button[disabled] {
    padding: 0 20px;
    height: 28px;
    background: #c1e0ff;
    color: #fafbff;
    outline: none;
    border-width: 0;
    border-radius: 4px;
    font-size: 12px;
  }
}
.handler {
  width: 90%;
  display: flex;
  justify-content: space-between;
  align-items: center;
    span {
      display: inline-block;
      width: 80px;
      text-align: center;
      font-size: 14px;
      font-weight: bold;
    }
    div:nth-child(2) {
      flex: 1;
    }
}
.menu-list {
  width: 100%;
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.bg {
  display: inline-block;
  border-radius: 5px;
  border: 2px solid #fff;
  width: 25px;
  height: 25px;
  cursor: pointer;
}
.color1 {
  background: #ec7070;
}
.color2 {
  background: #73d466;
}
.type-marker {
  display: flex;
  align-items: center;
  margin-right: 20px;
}
.recent-data {
  display: flex;
  flex-wrap: wrap;
  font-size: 14px;
  margin-bottom: 10px;
  .recent-item {
    height: 70px;
    display: flex;
    flex: 1;
    flex-direction: column;
    justify-content: center;
    border: 1px solid #aaa;
    text-align: center;
    border-radius: 10px;
    margin-top: 10px;
    margin-right: 10px;
    margin-left: 10px;
    // margin-right: 40px;
    padding: 10px 20px;
    box-sizing: border-box;
    cursor: pointer;
    & > div:nth-child(1) {
      line-height: 24px;
      b {
        font-size: 18px;
      }
    }
  }
}

</style>